package com.eshop.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.eshop.bean.GoodsBean;

public class GoodsModifyDao {

	private Connection conn = null;
	private PreparedStatement sta = null;
	private ResultSet r = null;
	
	public GoodsBean goodsquery(Integer goodsId)
	{
		GoodsBean bean = new GoodsBean();
		try {
			conn = Conn.getConn();
			String sql = "SELECT goods_img,goods_name,goods_remark,goods_price," +
					"goods_stock from goods WHERE goods_id = ?";
			sta = conn.prepareStatement(sql);
			sta.setObject(1, goodsId);
			r = sta.executeQuery();
			while(r.next())
			{
				String goodsImg = r.getString("goods_img");
				String goodsName = r.getString("goods_name");
				String goodsRemark = r.getString("goods_remark");
				Float goodsPrice = r.getFloat("goods_price");
				Integer goodsStock = r.getInt("goods_stock");
				
				bean.setGoodsImg(goodsImg);
				bean.setGoodsName(goodsName);
				bean.setGoodsRemark(goodsRemark);
				bean.setGoodsPrice(goodsPrice);
				bean.setGoodsStock(goodsStock);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(r != null)
				{
					r.close();
				}
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return bean;
	}
	
	public int goodsModify(String goodsName, String goodsRemark, Float goodsPrice, Integer goodsStock, Integer goodsId)
	{
		int i = 0;
		try {
			conn = Conn.getConn();
			String sql = "UPDATE goods SET goods_name = ?,goods_remark = ?,goods_price = ?," +
					"goods_stock = ? WHERE goods_id = ?";
			sta = conn.prepareStatement(sql);
			sta.setObject(1, goodsName);
			sta.setObject(2, goodsRemark);
			sta.setObject(3, goodsPrice);
			sta.setObject(4, goodsStock);
			sta.setObject(5, goodsId);
			i = sta.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(r != null)
				{
					r.close();
				}
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}
}
